﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ec_System.Models.Web;
using Ec_System.Bll.Web;
using Ec_System.API;

namespace Ec_System.Cache.Web
{
    public class ShopCartCache
    {
        /// <summary>
        /// 从缓存中读取Product信息
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public ShopProductData GetProductInfoByID(int productID)
        {
            //首先从缓冲中读取Product信息
            var item = RedisHelper.Get<ShopProductData>(productID.ToString());
            if(item == null)
            {
                //缓存不存在该Product时再同步数据库
                item = new VendorShopManager().GetProductInfoByID(productID);
                RedisHelper.Set<ShopProductData>(productID.ToString(), item, 3600);
            }

            return item;
        }

        /// <summary>
        /// 从缓存中读取购物车数据
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ShopCartData> GetShopCartByLoginID(string loginID )
        {
            return RedisHelper.Get<List<ShopCartData>>("S" + loginID.ToString());
        }

        /// <summary>
        /// 将购物车数据存到缓存中
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="items"></param>
        public void SetShopCartByLoginID(string loginID, List<ShopCartData> items)
        {
            RedisHelper.Set<List<ShopCartData>>("S" + loginID.ToString(), items, 3600);
        }

        /// <summary>
        /// 删除指定用户的购物车--全部数据
        /// </summary>
        /// <param name="loginID"></param>
        public void DeleteShopCartByLoginID(string loginID)
        {
            RedisHelper.Delete<List<ShopCartData>>("S" + loginID);
        }
    }
}
